package com.burst.reptile.modular.api.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.utils.poi.ExcelUtil;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.web.form.SortForm;
import com.burst.reptile.modular.zp.entity.ZpColumn;
import com.burst.reptile.modular.zp.service.IZpColumnService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 栏目表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-06-15
 */
@RestController
@RequestMapping("/zp/column")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpColumnController extends BaseController{

	private final IZpColumnService columnService;
	
	/**
	 * 获取栏目列表
	 * @param resources
	 * @return
	 */
	@GetMapping("/all")
	public Result<Object> all(){
		return Result.success(columnService.listAll());
	}
	
	/**
	 * 获取栏目列表
	 * @param resources
	 * @return
	 */
	@GetMapping("/list")
	@PreAuthorize("@ss.hasPermi('zp:column:list')")
	public LayuiResult<Object> list(ZpColumn column){
		List<ZpColumn> list = columnService.listByCloum(column);
		return LayuiResult.success(list.size(), list);
	}
	
	/**
	 * 添加栏目
	 * @param resources
	 * @return
	 */
	@PostMapping("/add")
	@PreAuthorize("@ss.hasPermi('zp:column:add')")
	@Log(title = "栏目管理", businessType = BusinessType.INSERT)
	public Result<Object> add(@Validated @RequestBody ZpColumn column){
		if (UserConstants.NOT_UNIQUE.equals(columnService.checkColumnKeyUnique(column))) {
			return Result.error("新增栏目'" + column.getColumnName() + "'失败，已存在，不能重复添加");
		}
		return Result.success(columnService.insertColumn(column));
	}
	
	/**
	 * 添加栏目
	 * @param resources
	 * @return
	 */
	@PutMapping("/edit")
	@PreAuthorize("@ss.hasPermi('zp:column:edit')")
	@Log(title = "栏目管理", businessType = BusinessType.UPDATE)
	public Result<Object> edit(@Validated @RequestBody ZpColumn column){
		if (UserConstants.NOT_UNIQUE.equals(columnService.checkColumnKeyUnique(column))) {
			return Result.error("栏目'" + column.getColumnName() + "'已存在");
		}
		return Result.success(columnService.updateColumn(column));
	}
	
	/**
	 * 批量删除标签集
	 * @param ids 标签ID集
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('zp:column:remove')")
	@Log(title = "栏目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove/{ids}")
	public Result<Object> remove(@PathVariable Integer[] ids) {
		return Result.success(columnService.deleteByIds(ids));
	}
	
	/**
	 * 导出栏目
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('zp:column:export')")
	@GetMapping("/export")
	public Result<Object> export() {
		List<ZpColumn> list = columnService.list(new QueryWrapper<ZpColumn>().orderByAsc("column_sort"));
		ExcelUtil<ZpColumn> util = new ExcelUtil<ZpColumn>(ZpColumn.class);
		return util.exportExcel(list, "栏目数据");
	}
	
	/**
	 * 排序
	 * @param id ID
	 * @param sort 排序号
	 * @return
	 */
	@PutMapping("/sort")
	@PreAuthorize("@ss.hasPermi('zp:banner:sort')")
	public Object sort(@Validated @RequestBody SortForm sort) {
		this.columnService.sort(sort);
		return SUCCESS;
	}
	
}
